<template>
  <div class="home">
    <ul>
      <li @click="goDetail(item)" v-for="item in banners" :key="item.id">
        <img :src="item.img" alt="good-img">
        <p>{{item.cont}}</p>
        <div>
          <span>￥<em>{{item.price}}</em>.00</span>
          <i @click.stop="addCart(item)"></i>
        </div>
      </li>
    </ul>

    <transition name="hide">
      <div class="alert" v-show="show">
        <section>√</section>
        <span>加入购物车成功</span>
      </div>
    </transition>
    
  </div>
</template>

<script>
// npm i module --save/ -S 这个命令会将依赖安装进入package.json文件的dependencies里面
// npm install module --save-dev /-D 这个命令会将依赖安装进入package.json文件的devDependencies里面
// npm install module  这个命令安装依赖不会进入依赖清单
import axios from 'axios'
import { useRouter } from 'vue-router'
import { ref } from 'vue'
import { useStore } from 'vuex'
export default {
  name: 'Home',

  setup() {
    const banners = ref([])
    let show = ref(false)
    axios.get('/data.json').then(res => {
      // console.log(res);
      banners.value = res.data
    })

    let store = useStore()

    // 加入购物车的函数
    function addCart(item) {
      show.value = true
      setTimeout(() => {
        show.value = false
      }, 500);
      store.commit('addGood', item)

    }

    let router = useRouter()
    // 进入详情页
    function goDetail(item) {
      // console.log(item.toString());
      router.push({
        path: 'detail',
        // query: {
        //   item: JSON.stringify(item)
        // }
        query: item
      })
    }
    return {
      banners,
      show,
      addCart,
      goDetail
    }
  }
}
// vue2中 动画进入的初始状态是v-enter
// v-enter-from 表示动画进入的初始状态
// v-enter-active 动画进入的过程状态
// v-leave-active 动画离开的过程状态
// v-leave-to 动画离开的结束状态
</script>
<style lang="scss">
* {
  box-sizing: border-box;
}
body ,html{
  margin: 0;
  width: 100%;
  height: 100%;
   background-color: #f6f6f6;
}
.home {
  width: 100%;
  padding-top: 20px;
}
.home ul {
  width: 100%;
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-wrap: wrap;
 
  padding: 0 15px;
  justify-content: space-between;
  li {
    flex-basis: calc((100% - 10px)/ 2);
    background-color: #fff;
    margin-top: 10px;
    padding-bottom: 10px;
    img {
      width: 100%;
      height: 200px;
    }
    p {
      margin-top: 10px;
      padding-left: 10px;
    }
    span {
      color: #d00000;
      font-size: 12px;
      
      em {
        font-size: 18px;
        font-style: normal;
      }
    }
    div {
      display: flex;
      widows: 100%;
      justify-content: space-between;
      align-items: center;
      padding: 10px;
      i {
        display: block;
        width: 20px;
        height: 20px;
        background-image: url('../assets/xq-foot.png');
        background-repeat: no-repeat;
        background-position: center;
        background-size: 100%;
      }
    }
  }
}
.alert {
  width: 150px;
  height: 120px;
  border-radius: 6px;
  background-color: rgba(0, 0, 0, 0.8);
  position: fixed;
  top: calc(50% - 60px);
  left: calc(50% - 75px);
  z-index: 2;
  color: #fff;
  text-align: center;
  section {
    width: 40px;
    height: 40px;
    text-align: center;
    line-height: 40px;
    border-radius: 40px;
    border: 1px solid #fff;
    margin: 20px auto;
  }
}

.hide-enter-from, .hide-leave-to {
  opacity: 0;
}

.hide-enter-active, .hide-leave-active {
  transition: opacity .5s linear;
}
</style>
